#include "process_pool.h"
int main(int argc,char *argv[])
{ 
    printf("AF_INET=%d\n",AF_INET);
    ARGS_CHECK(argc,3);
    int sockfd;
    sockfd=socket(AF_INET,SOCK_STREAM,0);
    ERROR_CHECK(sockfd,-1,"socket");
    printf("sockfd=%d\n",sockfd);
    struct sockaddr_in ser;
    bzero(&ser,sizeof(ser));
    ser.sin_family=AF_INET;
    ser.sin_addr.s_addr=inet_addr(argv[1]);
    ser.sin_port=htons(atoi(argv[2]));
    int ret;
    ret=connect(sockfd,(struct sockaddr*)&ser,sizeof(ser));
    ERROR_CHECK(ret,-1,"connect");
    int dataLen;
    char buf[1024]={0};
    recv(sockfd,&dataLen,4,0);
    recv(sockfd,buf,dataLen,0);
    char tmp[1024]={0};
    strncpy(tmp,buf,sizeof(tmp));
    int fd;
    for(int i=1;-1==(fd=open(tmp,O_WRONLY|O_CREAT|O_EXCL,0666));++i)
    {
        bzero(tmp,sizeof(tmp));
        sprintf(tmp,"%s%d",buf,i);
    }
    bzero(buf,sizeof(buf));
    while(1)
    {
        recv(sockfd,&dataLen,4,0);
        ret=recv(sockfd,buf,dataLen,0);
        //这里recv的返回值ret为从对端接受到的字节数
        //当ret=0的时候传输结束
        ERROR_CHECK(ret,-1,"recv");
        if(ret)
        {
            write(fd,buf,dataLen);
        }else{
            break;
        }
    }
    return 0;
}
